from io import RawIOBase
import pymysql
import requests
from selenium import webdriver
from time import sleep
from selenium.webdriver.chrome.options import Options
from selenium import webdriver

class drawDetail:
    def __init__(self):
        self.host = "192.168.0.113"
        self.port = 3306
        self.user='coname'
        self.password = 'xZS6p4LDcKCwn4Yb'
        self.db="co"
        self.charset="utf8"
        self.num=1
        options = Options()
        options.add_experimental_option('excludeSwitches', ['enable-automation'])
        self.bro = webdriver.Chrome(options=options)
        self.bro.maximize_window()

    def open_mysql(self):#连接数据库
        try:
            self.conn = pymysql.connect(host=self.host,port=self.port,user=self.user,passwd=self.password,db=self.db,charset=self.charset)
            self.cursor = self.conn.cursor()
            print("打开数据库成功!")
            return self.conn,self.cursor
        except Exception as e:
            print("打开数据库失败>>>",e)

    def close_mysql(self,conn,cursor):#关闭数据库
        try:
            cursor.close()
            conn.close()
            print("关闭数据库成功!")
        except Exception as e:
            print('关闭数据库失败>>>',e)

    def output_mysql(self):#取出数据
        conn,cursor = self.open_mysql()
        try:
            cursor.execute('select srname,fileurl from co_art')
            datas = cursor.fetchall()
            print("取出数据成功!")
        except Exception as e:
            print("取出数据失败>>>",e)
        self.close_mysql(conn,cursor)
        return datas

    def update_mysql(self,data):#更改数据情况
        conn,cursor = self.open_mysql()
        try:
            cursor.execute('update co_art set introduction="{}",contact="{}",painter="{}" where srname="{}"'.format(data[1],data[2],data[3],data[0]))
            conn.commit()
            print("更新画廊数据成功!")
        except Exception as e:
            print("更新画廊数据失败>>>",e)
        self.close_mysql(conn,cursor)   

    def visit_page_index(self):#访问网站首页
        datas = self.output_mysql()
        for i in range(0,len(datas),1):
            self.bro.get(datas[i][1])
            
            sleep(5)
    
    def visit_page_introduction(self):#简介
        pass

    def visit_page_contact(self):#联系方式
        pass

    def visit_page_painter(self):#获取画家
        pass

    def run(self):
        self.visit_page_index()

if __name__ == '__main__':
    drawDetail().run()